import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/user/User.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import List from '../components/goods/List.vue'
import Add from '../components/goods/Add.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'
// import Home from '../views/Home.vue'

Vue.use(VueRouter)
// 路由规则
const routes = [
  { path: '/', redirect: '/login' }, // 重定向地址
  { path: '/login', component: Login },
  { 
    path: '/home', 
    component: Home,
    redirect: '/welcome',  //重定向到welcome
    children: [{ path: '/welcome', component: Welcome },
    { path: '/users', component: User},//用户管理  用户列表
    { path: '/rights', component: Rights},//权限管理 权限列表
    { path: '/roles', component: Roles},//权限管理   角色列表
    { path: '/categories', component:Cate},  //商品管理  商品分类
    { path: '/params' , component:Params},//商品管理  分类参数
    { path: '/goods' , component:List},//商品管理  商品列表
    { path: '/goods/add' , component:Add },//商品管理 添加商品组件
    { path: '/orders' , component:Order},//订单管理   订单列表组件
    { path: '/reports' , component:Report},//数据统计  数据报表
  ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to,from,next) => {
  //  to将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行  next()放行 next('./login)强制跳转
  if(to.path === '/login') return next();  //如果地址是login  就放行
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token');
  if(!tokenStr) return next('/login');//如果不是登录页面  强制跳转到登录页面
  next();
})

export default router
